//
//  CaseViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/7.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "CaseViewController.h"
#import "NSHandle.h"
#import "CaseCollectionViewCell.h"
#import "DescribeCollectionViewCell.h"
#import "CaseModel.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
@interface CaseViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//@property (strong, nonatomic)UICollectionView *mainCollectionView;
@property (strong, nonatomic) IBOutlet UICollectionView *mainCollectionView;

@property (strong, nonatomic) IBOutlet UIImageView *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *name;
//存储该页面id
@property(nonatomic,strong)NSString *pageId;

@property (strong, nonatomic) IBOutlet UIImageView *backImageView;
@property (strong, nonatomic) IBOutlet UIImageView *shareImageView;
@property (strong, nonatomic) IBOutlet UIImageView *collectImageView;


@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)UIImageView *image111;
@end

@implementation CaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
   // self.navigationController.navigationBar.hidden = YES;
    
    UITapGestureRecognizer *backTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backAction:)];
    UITapGestureRecognizer *shareTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shareAction:)];
    UITapGestureRecognizer *collectTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(collectAction:)];
    [self.backImageView addGestureRecognizer:backTap];
    [self.shareImageView addGestureRecognizer:shareTap];
    [self.collectImageView addGestureRecognizer:collectTap];
    
/*
//    //1创建layout对象
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//    //2配置属性
//    //(2.1设置行间距(每个item上下距离)
//    layout.minimumLineSpacing = 10;
//    //(2.2设置左右间距(列间距
//    layout.minimumInteritemSpacing =5;//>=50
//    //(2.3 设置item的size
//    //layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width-20,250);
//    //(2.4设置距屏幕边缘的距离
//    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
//    //3创建 UICollectionView
//    self.mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height)];
//    //4添加到父视图
//    [self.view addSubview:self.mainCollectionView];
   */
    //5遵守协议 实现方法
    //设置代理
    self.mainCollectionView.backgroundColor =[UIColor whiteColor];
    
    self.mainCollectionView.dataSource =self;
    self.mainCollectionView.delegate = self;
    
    
    
      NSLog(@"%@",self.ids);
    [[NSHandle shareHandle]getDataWithStrUrl:[NSString stringWithFormat:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/GetMealsInfo/id/%@",self.ids] WithHTTPMethod:nil WithHTTPBody:nil WithBlock:^(BOOL Finished) {
        if (Finished) {
            [self.mainCollectionView reloadData];
        }
    }];
    
    
   
}

-(void)backAction:(UITapGestureRecognizer *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = NO;
}
-(void)shareAction:(UITapGestureRecognizer *)sender{
    NSLog(@"分享");
}
-(void)collectAction:(UITapGestureRecognizer *)sender{
    NSLog(@"收藏");
}

//进店看看
- (IBAction)ComeToSeeSee:(id)sender {
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    detailVC.ids = self.pageId;
    [self.navigationController pushViewController:detailVC animated:YES];
    self.navigationController.navigationBar.hidden = NO;
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[NSHandle shareHandle].dataArray count];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    //NSLog(@"-------%lu",(unsigned long)[[NSHandle shareHandle].dataArray count]);
    return 1 ;

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section == 0) {
//        UINib *nib = [UINib nibWithNibName:@"DescribeCollectionViewCell" bundle:[NSBundle mainBundle]];
//        [collectionView registerNib:nib forCellWithReuseIdentifier:@"DescribeCollectionViewCell"];
//        DescribeCollectionViewCell *cell = [[DescribeCollectionViewCell alloc]init];
//        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DescribeCollectionViewCell" forIndexPath:indexPath];
//        CaseModel *model = [NSHandle shareHandle].dataArray[indexPath.section];
//        cell.titleLabel.text = model.title;
//        cell.describeLabel.text = model.describe;
//        return cell;
//    }if (indexPath.section > 0 && indexPath.section < [[NSHandle shareHandle].dataArray count] + 2) {
    if (indexPath.row == 0) {
        UINib *nib = [UINib nibWithNibName:@"DescribeCollectionViewCell" bundle:[NSBundle mainBundle]];
                [collectionView registerNib:nib forCellWithReuseIdentifier:@"DescribeCollectionViewCell"];
                DescribeCollectionViewCell *cell = [[DescribeCollectionViewCell alloc]init];
                cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DescribeCollectionViewCell" forIndexPath:indexPath];
        CaseModel *model = [NSHandle shareHandle].dataArray.firstObject;
        [cell setModel:model];
        [self.logoImage sd_setImageWithURL:[NSURL URLWithString:model.logo_path]];
        self.logoImage.layer.cornerRadius = 20;
        self.name.text = model.name;
        self.pageId = model.ids;
        return cell;
    }else{
        UINib *nib = [UINib nibWithNibName:@"CaseCollectionViewCell" bundle:[NSBundle mainBundle]];
        [collectionView registerNib:nib forCellWithReuseIdentifier:@"CaseCollectionViewCell"];
        CaseCollectionViewCell *cell = [[CaseCollectionViewCell alloc]init];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CaseCollectionViewCell" forIndexPath:indexPath];
        CaseModel *model = [NSHandle shareHandle].dataArray[indexPath.row];
        [cell.mainImage sd_setImageWithURL:[NSURL URLWithString:model.media_path] placeholderImage:[UIImage imageNamed:@"aaaaa.jpg"]];
        // NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.media_path]];
        // UIImage *image = [UIImage imageWithData:data];
        
        //NSLog(@"+++++++宽%f,高%f",image.size.width,image.size.height);
        
        //         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.media_path]];
        //         UIImage *image = [UIImage imageWithData:data];
        //        CGFloat height = image.size.height;
        //        CGFloat width = image.size.width;
        //        CGFloat newHeight = height * [UIScreen mainScreen].bounds.size.width-20 / width;
        
        return cell;
    }
    

//    }
//   
//    return nil;
}

//对每个item进行设置大小
//实现对图片等比铺放(防止变形)
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CaseModel *model = [NSHandle shareHandle].dataArray.firstObject;
        CGFloat height = [model cellHeight];
         CGSize size = {[UIScreen mainScreen].bounds.size.width-20,height+120};
        return size;
    }else{
        CaseModel *model = [NSHandle shareHandle].dataArray[indexPath.row];
        
        //    CGFloat height = image.size.height;
        //    CGFloat width = image.size.width;
        CGFloat newHeight = [model.height floatValue] * ([UIScreen mainScreen].bounds.size.width-20) / [model.width floatValue];
        
        // NSLog(@"宽%@,高%@",model.width,model.height);
        // NSLog(@"新%f,宽%@,高%@",newHeight,model.width,model.height);
        
        // NSLog(@"-------宽%@,高%@",model.width,model.height);
        CGSize size = {[UIScreen mainScreen].bounds.size.width-20,200};
        if ([model.width isEqualToString:@"0"] || model.width.length == 0) {
            // NSLog(@"000000");
            return size;
            
        }else{
            CGSize size2 = {[UIScreen mainScreen].bounds.size.width-20,newHeight};
            //        NSLog(@"+++++++++++");
            return size2;
        }
        //    return size;

    }
    
    }

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return     UIEdgeInsetsMake(10, 10, 10, 10);

}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
